//
//  SISSMyCooperationRequestPassengerViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/8.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyCooperationRequestPassengerViewController.h"
#import "SISSMinePassengerCooperationCell.h"
#import "SISSPassengerCooperationDetailViewController.h"
#import "SISSChatViewController.h"
#import "SISSPassengerCooperationModel.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSChatViewController.h"

static NSString *myCooperationRequestPassengerCell = @"myCooperationRequestPassengerCell";
@interface SISSMyCooperationRequestPassengerViewController ()<SISSMinePassengerCooperationCellDelegate>
@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 房源合作数组 */
@property (strong, nonatomic) NSArray<SISSPassengerCooperationModel *> *passengerCooperationArr;

@end

@implementation SISSMyCooperationRequestPassengerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestMyRequestPassengerCooperationData];
    WeakSelf
    
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf requestMyRequestPassengerCooperationData];
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.passengerCooperationArr.count];
    return self.passengerCooperationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SISSMinePassengerCooperationCell *cell = [tableView dequeueReusableCellWithIdentifier:myCooperationRequestPassengerCell forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.passengerCooperationModel = self.passengerCooperationArr[indexPath.row];
    
    return cell;
}

#pragma mark - SISSMinePassengerCooperationCellDelegate
- (void)passengerCooperationCell:(SISSMinePassengerCooperationCell *)cell clickContactButton:(UIButton *)button {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.passengerCooperationArr[indexPath.row].imAccount conversationType:EMConversationTypeChat];
    SISSContactModel *model = [[SISSContactModel alloc] init];
    model.name = self.passengerCooperationArr[indexPath.row].name;
    model.portrait = self.passengerCooperationArr[indexPath.row].portrait;
    model.spId = self.passengerCooperationArr[indexPath.row].spId;
    model.accountType = 1;
    chatVC.contactModel = model;
    chatVC.title = self.passengerCooperationArr[indexPath.row].name;
    [self.navigationController pushViewController:chatVC animated:YES];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return 153;
//}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    SISSPassengerCooperationDetailViewController *vc = [[SISSPassengerCooperationDetailViewController alloc] init];
//    vc.isFindCooperation = YES;
//    vc.title = @"客源合作详情";
//    [self.navigationController pushViewController:vc animated:YES];
    
    
    //    NSDictionary *parameters = @{
    //                                 @"adcrId":self.passengerCooperationArr[indexPath.row].acrId
    //                                 };
    //    [kNetworkManager requestPassengerCooperationDetailWithparameters:parameters success:^(SISSPassengerCooperationDetailModel *passengerCooperationModel) {
    //
    //        SISSPassengerCooperationDetailViewController *vc = [[SISSPassengerCooperationDetailViewController alloc] init];
    //        vc.isFindCooperation = YES;
    //        vc.title = @"客源合作详情";
    //        vc.passengerCooperationModel = passengerCooperationModel;
    //        [self.navigationController pushViewController:vc animated:YES];
    //
    //    } failed:^(NSError *error) {
    //        
    //        NSLog(@"%@",error);
    //    }];
    
    
}

- (void)requestMyRequestPassengerCooperationData {
    
    WeakSelf;
    [kNetworkManager requestMyRequestPassengerCooperationWithsuccess:^(NSArray<SISSPassengerCooperationModel *> *passengerCooperations) {
        
        weakSelf.passengerCooperationArr = passengerCooperations;
        
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 153;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSMinePassengerCooperationCell" bundle:nil] forCellReuseIdentifier:myCooperationRequestPassengerCell];
    
}
@end
